package siddur.traktask.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class TaskUtil {

	/*
	 * token = create date + id
	 */
	
	private static final char[] SPE_CHARS = {
		'$', '-', '=', '+', '@', ':', '~'
	};
	
	private static char[] CHARS;
	
	static{
		List<Character> list = new ArrayList<>();
		//special
		for (int i = 0; i < SPE_CHARS.length; i++) {
			list.add(SPE_CHARS[i]);
		}
		
		//0 ~ 9
		for (int i = 48; i <= 57; i++) {
			list.add((char)i);
		}
		
		//A ~ Z
		for (int i = 65; i <= 90; i++) {
			list.add((char)i);
		}
		
		//a ~ z
		for (int i = 97; i <= 122; i++) {
			list.add((char)i);
		}
		
		CHARS = new char[list.size()];
		for (int i = 0; i < CHARS.length; i++) {
			CHARS[i] = list.get(i);
		}
	}
	
	private static String toString(long v){
		int len = CHARS.length;
		StringBuilder sb = new StringBuilder();
		while(true){
			int mod = (int)(v % len);
			sb.append(CHARS[mod]);
			v = (v - mod) / len;
			if(v == 0){
				break;
			}
		}
		return sb.reverse().toString();
	}
	
	public static long toLong(String s){
		int len = CHARS.length;
		long l = 0;
		for (int i = 0; i < s.length(); i++) {
			l *= len; 
			int in = toInt(s.charAt(i));
			if(in == -1){
				return -1;
			}
			l += in;
		}
		return l;
	}
	
	private static int toInt(char c){
		for (int i = 0; i < CHARS.length; i++) {
			if(c == CHARS[i]){
				return i;
			}
		}
		return -1;
	}
	
	public static String generateToken(long id, long time){
		String token = toString(time) + "_" + toString(id);
		return token;
	}
	
	public static void main(String[] args) {
		for (int i = 97; i < 122; i++) {
			System.out.println((char)i);
		}
	}
}
